package com.example.cloudmusicserve.util;

import com.example.cloudmusicserve.dao.MusicDao;
import com.example.cloudmusicserve.entity.RecentPlay;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;

/**
 * @description: FilerRecentPlay
 * @author: xiaoyuan
 * @time: 2023/5/29
 */
@Service
public class FilerRecentPlay {

    //注入MusicDao
    @Resource
    MusicDao musicDao;

    public void deleteOvertimeMusic(RecentPlay recentPlay){
        //通过username和musicId查询
        Integer flag = musicDao.queryByMusicIdAndUsername(recentPlay);
        if(flag==1){
          musicDao.deleteCommonData(recentPlay);
        }
        //历史记录过期的进行一个删除
        List<RecentPlay> list = musicDao.queryByUsername(recentPlay);
        for (int i = 0; i < list.size(); i++) {
            RecentPlay r = list.get(i);
            //获取当前时间戳
            long l = System.currentTimeMillis();
            String l1 = String.valueOf(l);
            BigInteger Dvalue = new BigInteger(r.getEndTime()).subtract(new BigInteger(l1));
            //判断该时间差
            if(Dvalue.compareTo(new BigInteger("0"))<=0){
                    musicDao.deleteOverTimeMusic(recentPlay);
            }
        }
    }
}
